using System.Collections;
using System.IO;


namespace EpisodeWizard.Core
{

    enum ImportErrorCode
    {
        None = 0,
        TooManyFiles = 1,
        GeneralError = 2
    };


    class DirectoryWalker
    {
        const int _maxFileNumber = 1000;

        ArrayList _fileList = new ArrayList();
        public ArrayList FileList
        {
            get { return _fileList; }
        }

        ImportErrorCode _errorCode;
        public ImportErrorCode ErrorCode
        {
            get { return _errorCode; }
        }

        public void WalkDirectories(string baseDir)
        {
            WalkDirectories(baseDir, "*.*");
        }

        public void WalkDirectories(string baseDir, string fileFilter)
        {
            try
            {
                WalkFiles(baseDir, fileFilter);
                if (_errorCode == ImportErrorCode.None)
                {
                    foreach (string dirName in Directory.GetDirectories(baseDir))
                    {
                        WalkDirectories(dirName, fileFilter);
                        if (_errorCode != ImportErrorCode.None)
                        {
                            break;
                        }
                    }
                }
            }
            catch (IOException)
            {
                _errorCode = ImportErrorCode.GeneralError;
            }
        }

        public void WalkFiles(string baseDir)
        {
            WalkFiles(baseDir, "*.*");
        }

        public void WalkFiles(string baseDir, string fileFilter)
        {
            foreach (string fileName in Directory.GetFiles(baseDir, fileFilter))
            {
                if (_fileList.Count >= _maxFileNumber)
                {
                    _errorCode = ImportErrorCode.TooManyFiles;
                    return;
                }
                _fileList.Add(fileName);
            }
            _errorCode = (_fileList.Count >= _maxFileNumber) ?
                ImportErrorCode.TooManyFiles : ImportErrorCode.None;
        }
    }


}

